<?php


class PostController extends Controller
{
	public $code_url;
	
	public function actionIndex()
	{
	/*
		$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );

		if(!empty($_SESSION['token']['access_token'])){
			$code_url = Yii::app()->createUrl('/site/index');
		}else{
			$code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );
		}
		*/
		//echo $code_url;
		//$this->code_url=$code_url;
		//$this->code_url=$code_url;

		$this->render('index');
	}
	
	/*
	 *添加路书
	 *
	 */
	public function actionCreateBook()
	{
		$model=new Book;
		if(isset($_POST['Post']))
		{
			$model->attributes=$_POST['Post'];
			$model->uid = $_SESSION['token']['uid'];
			if($model->save())
				$this->redirect(array('post/create'));
		}
		
		$this->render('/book/create',array(
			'model'=>$model,
		));
		
	}
	
	/*
	 *添加路书内容
	 *
	 */
	public function actionCreate()
	{
		$model=new Post;
		if(isset($_POST['Post']))
		{
			$model->attributes=$_POST['Post'];
			$uploadFile = CUploadedFile::getInstance($model, 'image_url');
			if($uploadFile !== null){
				$uploadFileName = mktime() . '.' . $uploadFile->getExtensionName();
				$model->image_url = $uploadFileName;
			}
			
			$model->uid = $_SESSION['token']['uid'];
			if($model->save()){
				$uploadFile->saveAs(Yii::app()->basePath . '/upload/'.$uploadFileName);
				$this->redirect(array('post/create'));
			}
		}
		
		
	}
	/*
	 *向微博发送消息
	 *
	 */
	public function actionSendWeibo()
	{
	
		$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );
		$ms  = $c->home_timeline(); // done
		$uid_get = $c->get_uid();
		$uid = $uid_get['uid'];
		$user_message = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息
		if( isset($_REQUEST['text']) ) {
			$ret = $c->update( $_REQUEST['text'] );	//发送微博
			if ( isset($ret['error_code']) && $ret['error_code'] > 0 ) {
				echo "<p>发送失败，错误：{$ret['error_code']}:{$ret['error']}</p>";
			} else {
				echo "<p>发送成功</p>";
			}
		}
		
		//do
	}
	/*
	 *向论坛发送消息
	 *
	 */
	 public function actionSendBbs()
	 {
	 
	 
	 }

}


?>

